/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import model.pojo.Post;
import model.pojo.PostTag;
import model.pojo.User;
import model.pojo.util.helper;

/**
 *
 * @author Yasith
 */
public class fbPost {

    public static Post addPost(User author, String title, String text, String status, Collection<PostTag> tags) {

        EntityManager em = helper.getEMF().createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();

        Post p = new Post();
        p.setUserID(author);
        p.setPostTitle(title);
        p.setPostText(text);
        p.setPostStatus(status);
        p.setTimestamp(new Date());
        p.setPosttagsCollection(tags);
        em.persist(p);
        tx.commit();
        em.close();
        return p;
    }
    
        public static Post updatePost(Post p) {

        EntityManager em = helper.getEMF().createEntityManager();
        EntityTransaction tx = em.getTransaction();
        tx.begin();

        Post p2 = em.find(Post.class, p.getPostId());
        p2.setUserID(p.getUserID());
        p2.setPostTitle(p.getPostTitle());
        p2.setPostText(p.getPostText());
        p2.setPostStatus(p.getPostStatus());
        p2.setPosttagsCollection(p.getPosttagsCollection());
        
        em.persist(p2);
        tx.commit();
        em.close();
        return p2;
    }


    public static List<Post> getPosts(User u) {
        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createNamedQuery("Post.findByUserID");
        query.setParameter("userID", u);
        List<Post> p;
        try {
            p = (List<Post>) query.getResultList();
        } catch (NoResultException ex) {
            p = null;
        }
        newTx.commit();
        newEm.close();

        return p;
    }
    
        public static List<Post> getPosts(int count) {
        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createQuery("SELECT p FROM Post p ORDER BY p.timestamp DESC");
        List<Post> p;
        try {
            p = (List<Post>) query.getResultList();
        } catch (NoResultException ex) {
            p = null;
        }
        newTx.commit();
        newEm.close();

        if(p.size() > count){
        return p.subList(0, count);
        }else{
            return p;
        }
    }

    public static Post getPost(int postId) {
        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createNamedQuery("Post.findByPostId");
        query.setParameter("postId", postId);
        Post p;
        try {
            p = (Post) query.getSingleResult();
        } catch (NoResultException ex) {
            p = null;
        }
        newTx.commit();
        newEm.close();

        return p;
    }

    public static boolean removePost(int postId) {
        EntityManager newEm = helper.getEMF().createEntityManager();
        EntityTransaction newTx = newEm.getTransaction();
        newTx.begin();
        Query query = newEm.createNamedQuery("Post.findByPostId");
        query.setParameter("postId", postId);
        Post p;
        try {
            p = (Post) query.getSingleResult();
        } catch (NoResultException ex) {
            p = null;
        }
        newEm.remove(p);

        newTx.commit();
        newEm.close();

        return true;
    }
}
